package com.handinfo.android.game.item;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.game.IIcon;
import com.handinfo.android.game.IShortcutObject;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.game.Role;
import com.handinfo.android.game.skill.SkillConstants;
import com.handinfo.android.ui.window.UIFixWindow;
import com.handinfo.android.utils.Tools;

public class Item implements IIcon, IShortcutObject
{

	// 道具的标示
	public static final int ID_NONE = -1;// 道具Id
	// 道具图标
	public Bitmap m_icon;
	public Bitmap m_pinzhi;
	public Bitmap m_bangding;
	
	public boolean m_dpinzhi;//是否绘制品质图片  默认绘制
	public boolean m_dbangding;//是否绘制绑定锁  默认绘制

	  //基础属性
    public long m_guid = 0;//道具唯一id
    public String m_key;//道具key
    public String m_name;//道具名字
    public int m_iconId;//icon
    public int m_border;//边框 
    public String m_descl;//道具描述
    public int m_maxHeapNum;//最大堆叠数
    public long m_maxHoldNum;//最大可持有数
    public int m_num;//道具数量
    public long m_birthday;//道具创建日期
    public long m_lifecycle;//持有时限 <秒>
    public int m_quality;//道具品质，稀有度
    public int m_operateType;//道具操作类型
    public int m_type;//道具类型
    public int m_subtype;//道具子类型
    public int m_bindingType;//道具绑定类型
    public int m_bindingState;//道具绑定状态
    public int m_maxUnbindingTimes;//最大可解绑次数
    public int m_usedUnbindingTimes;//已经使用过的解绑次数
    public int m_goldPrice;//卖店价格 ，商店购买此价格x4
    public int m_zhangongPrice;//战功售价
    public int m_yuanbaoPrice;//元宝售价
    public int m_banggongPrice;//帮贡售价
    public int m_minLevelLimit;//最小等级限制
    public int m_maxLevelLimit;//最大等级限制
    public int m_jobLimit;//职业限制
    public boolean m_isLimit;//是否手职业限制
    public int m_label_left;//商城左标签
    public int m_label_right;//商城右标签
    public boolean m_showCD;//是否绘制CD
    
    //售价情况
    public SellItem m_sell;


    
	public Item Clone()
	{
		Item item = null;
		switch (m_type) {
			case ItemConstants.道具类型_装备:
				ItemEquipment it = (ItemEquipment)this;
				item = new ItemEquipment();
				ItemEquipment equip = (ItemEquipment) item;
				 equip.m_quality = it.m_quality;
				 equip.m_equip_Index = it.m_equip_Index;
				 equip.m_maxDurable = it.m_maxDurable;
				 equip.m_nowDurable = it.m_nowDurable;
				 equip.m_gs = it.m_gs;
				 equip.m_suitKey = it.m_suitKey;
				 byte equipAttrsize = it.m_equipAttrSize;
				 equip.m_equipAttrSize = equipAttrsize;
				 equip.m_attrValue = new int[equipAttrsize];
				 equip.m_attrAddValue = new int[equipAttrsize];
				 equip.m_attrAddValus_forge = new int[equipAttrsize];
				 for(int i=0;i<equipAttrsize;i++){
					 equip.m_attrValue[i] = it.m_attrValue[i];
					 equip.m_attrAddValue[i]= it.m_attrAddValue[i];
					 equip.m_attrAddValus_forge[i] = it.m_attrAddValus_forge[i];
				 }
				 equip.m_strengthenLevel = it.m_strengthenLevel;
				 equip.m_product_order = it.m_product_order;
				 equip.m_maxStrengthenLevel = it.m_maxStrengthenLevel;
				 equip.m_nowStrengthenSpiritNeedAmount = it.m_nowStrengthenSpiritNeedAmount;
				 byte equipAsize = it.m_equipAnimationSize;
				 equip.m_equipAnimationSize = equipAsize;
				 equip.m_anim_url = new String[equipAsize];
				 for(int i=0;i<equipAsize;i++){
					 equip.m_anim_url[i] = new String(it.m_anim_url[i]);
				 }
				 byte equipGsize = it.m_equipGemsSize;
				 equip.m_equipGemsSize = equipGsize;
				 if( equipGsize > 0 ){ 
					 equip.m_hasGem = new byte[equipGsize];
					 equip.m_iconidGem = new short[equipGsize];
					 equip.m_nameGem = new String[equipGsize];
					 equip.m_GemData = new String[equipGsize];
					 equip.m_gemIcon = new Bitmap[equipGsize];
					 equip.m_gemSamllIcon = new Bitmap[equipGsize];
					 for(int i=0;i<equipGsize;i++){
						 byte hasGem = it.m_hasGem[i];
						 equip.m_hasGem[i] = hasGem;
						 if( hasGem == ItemConstants.含有宝石_是 ){
							 equip.m_iconidGem[i] = it.m_iconidGem[i];
							 equip.m_nameGem[i] = it.m_nameGem[i];
							 equip.m_GemData[i] = it.m_GemData[i];
							 equip.m_gemIcon[i] = IconManger.getInstance().getIcon(equip.m_iconidGem[i]);
						 }else{
							 equip.m_GemData[i] = equip.getGemDatas(-1);
						 }
						 equip.m_gemSamllIcon[i] = IconManger.getInstance().getEquipGemIcon(i, hasGem);
					 }
				 }
				 item.m_pinzhi = IconManger.getInstance().getPinzhi(it.m_quality);
				break;
			case ItemConstants.道具类型_消耗:
				item = new ItemConsume();
				ItemConsume comsume = (ItemConsume) item;
				comsume.m_cd_type = ((ItemConsume)this).m_cd_type;
				comsume.m_time_cd = ((ItemConsume)this).m_time_cd;
				break;
			case ItemConstants.道具类型_宝石:
				item = new ItemGem();
				ItemGem gem = (ItemGem) item;
				gem.m_needMount = ((ItemGem)this).m_needMount;
				gem.m_commposekey = ((ItemGem)this).m_commposekey;
				gem.m_attrindex = ((ItemGem)this).m_attrindex;
				gem.m_attrvalue = ((ItemGem)this).m_attrvalue;
				break;
			case ItemConstants.道具类型_其他:
				item = new ItemOther();
				break;
			default:
				item = new Item();
				break;

		}
		item.m_guid = m_guid;//ID 唯一标识
		item.m_key =  m_key;
		item.m_name = m_name;
		item.m_iconId = m_iconId;
		item.m_border = m_border;//边框
		item.m_descl = m_descl;//描述
		item.m_maxHeapNum = m_maxHeapNum;//最大堆叠数目
		item.m_maxHoldNum = m_maxHoldNum;//最大可持有数目
		item.m_num = m_num;//道具数目
		item.m_birthday =  m_birthday;//道具创建日期
		item.m_lifecycle = m_lifecycle;//有效时间
		item.m_quality = m_quality;//道具品质，稀有度
		item.m_operateType = m_operateType;//道具的操作类型
		item.m_type = m_type;//道具类型
		item.m_subtype = m_subtype;//道具子类型
		item.m_bindingType = m_bindingType;//道具绑定类型
		item.m_bindingState = m_bindingState;//道具绑定状态
		item.m_maxUnbindingTimes = m_maxUnbindingTimes;//最大可解绑次数
		item.m_usedUnbindingTimes = m_usedUnbindingTimes;//已经使用了的解绑次数
		item.m_goldPrice = m_goldPrice;//道具卖店价格
		item.m_zhangongPrice = m_zhangongPrice;//战功售价
		item.m_yuanbaoPrice = m_yuanbaoPrice;//元宝售价
		item.m_banggongPrice = m_banggongPrice;//帮贡售价
		item.m_minLevelLimit =  m_minLevelLimit;//最小等级限制
		item.m_maxLevelLimit = m_maxLevelLimit;//最大等级限制
		item.m_jobLimit = m_jobLimit;
		item.m_label_left = m_label_left;
		item.m_label_right = m_label_right;
		item.m_icon = IconManger.getInstance().getIcon(m_iconId);
		item.m_bangding = IconManger.m_bangding;
		if(item.m_jobLimit > 0 && item.m_jobLimit != DWGameManager.getInstance().m_role.m_vocation){
			item.m_isLimit = true;
		}
		return item;
	}

	public static final Item recvItemFormServer(DataInputStream dis) throws IOException
	{
		long guid = dis.readLong();//ID 唯一标识
		String key = dis.readUTF();
		String name = dis.readUTF();
		short iconId = dis.readShort();
		short bordered = dis.readShort();//边框
		String desc = dis.readUTF();//描述
		short MaxHeapNum = dis.readShort();//最大堆叠数目
		int MaxHoldNum = dis.readInt();//最大可持有数目
		short num = dis.readShort();//道具数目
		long Birthday = dis.readLong();//道具创建日期
		long Lifecycle = dis.readLong();//有效时间
		byte Quality = dis.readByte();//道具品质，稀有度
		byte OperateType = dis.readByte();//道具的操作类型
		byte Type = dis.readByte();//道具类型
		byte SubType = dis.readByte();//道具子类型
		byte BindingType = dis.readByte();//道具绑定类型
		byte BindingState = dis.readByte();//道具绑定状态
		byte MaxUnbindingTimes = dis.readByte();//最大可解绑次数
		byte UsedUnbindingTime = dis.readByte();//已经使用了的解绑次数
		int Goldprice = dis.readInt();//道具卖店价格
		int zhangongPrice = dis.readInt();//战功售价
		int yuanbaoPrice = dis.readInt();//元宝售价
		int banggongPrice = dis.readInt();//帮贡售价
		int minLevelLimit = dis.readInt();//最小等级限制
		int maxLevelLimit = dis.readInt();//最大等级限制
		int jobLimit = dis.readByte();//职业限制 0所有
		int lbleft = dis.readByte();
		int lbright = dis.readByte();
		Tools.debugPrintln("道具名称："+name);
		
		Item item = null;
		switch (Type) {
			case ItemConstants.道具类型_装备:
				 item = new ItemEquipment();
				 ItemEquipment equip = (ItemEquipment)item;
				 equip.m_quality = Quality;
				 equip.m_equip_Index = dis.readByte();
				 equip.m_maxDurable = dis.readShort();
				 equip.m_nowDurable = dis.readShort();
				 equip.m_gs = dis.readInt();
				 equip.m_suitKey = dis.readUTF();
				 byte equipAttrsize = dis.readByte();
				 equip.m_equipAttrSize = equipAttrsize;
				 equip.m_attrValue = new int[equipAttrsize];
				 equip.m_attrAddValue = new int[equipAttrsize];
				 equip.m_attrAddValus_forge = new int[equipAttrsize];
				 //Tools.debugPrintln(equip.m_suitKey);
					Tools.debugPrintln("属性赋值："+equipAttrsize);
				 for(int i=0;i<equipAttrsize;i++){
					 equip.m_attrValue[i] = dis.readInt();
					 equip.m_attrAddValue[i]= dis.readInt();
					 equip.m_attrAddValus_forge[i] = dis.readInt();
					 //Tools.debugPrintln(""+equip.m_attrValue[i]);
				 }
				 equip.m_strengthenLevel = dis.readShort();
				 equip.m_product_order = equip.getProductOrder(equip.m_strengthenLevel);
				 equip.m_maxStrengthenLevel = dis.readShort();
				 equip.m_nowStrengthenSpiritNeedAmount = dis.readShort();
				 byte equipAsize = dis.readByte();
				 equip.m_equipAnimationSize = equipAsize;
				 equip.m_anim_url = new String[equipAsize];
				 for(int i=0;i<equipAsize;i++){
					 equip.m_anim_url[i] = dis.readUTF();
				 }
				 byte equipGsize = dis.readByte();
				 equip.m_equipGemsSize = equipGsize;
				 //Tools.debugPrintln("equip.m_name="+equip.m_name+";equip.m_equipGemsSize="+equip.m_equipGemsSize);
				 if( equipGsize > 0 ){ 
					 equip.m_hasGem = new byte[equipGsize];
					 equip.m_iconidGem = new short[equipGsize];
					 equip.m_nameGem = new String[equipGsize];
					 equip.m_GemData = new String[equipGsize];
					 equip.m_gemIcon = new Bitmap[equipGsize];
					 equip.m_gemSamllIcon = new Bitmap[equipGsize];
					 for(int i=0;i<equipGsize;i++){
						 byte hasGem = dis.readByte();
						 equip.m_hasGem[i] = hasGem;
						 Tools.debugPrintln(" equip.m_hasGem["+i+"]="+equip.m_hasGem[i]);
						 if( hasGem == ItemConstants.含有宝石_是 ){
							 equip.m_iconidGem[i] = dis.readShort();
							 equip.m_nameGem[i] = dis.readUTF();
							 equip.m_GemData[i] = equip.getGemDatas(i);
							 equip.m_gemIcon[i] = IconManger.getInstance().getIcon(equip.m_iconidGem[i]);
						 }else{
							 equip.m_GemData[i] = equip.getGemDatas(-1);
						 }
						 equip.m_gemSamllIcon[i] = IconManger.getInstance().getEquipGemIcon(i, hasGem);
					 }
				 }
				 equip.m_attrLv = dis.readByte();
				 item.m_pinzhi = IconManger.getInstance().getPinzhi(Quality);
				 
				break;
			case ItemConstants.道具类型_消耗:
				 item = new ItemConsume();
				 ItemConsume comsume = (ItemConsume)item;
				 comsume.m_cd_type = dis.readByte();
				 comsume.m_time_cd = dis.readLong();
				break;
			case ItemConstants.道具类型_宝石:
				item = new ItemGem();
				ItemGem gem = (ItemGem)item;
				gem.m_needMount = dis.readByte();
				gem.m_commposekey = dis.readUTF();
				gem.m_attrindex = dis.readInt();
				gem.m_attrvalue = dis.readInt();
				break;
			case ItemConstants.道具类型_其他:
				item = new ItemOther();
				break;
			default:
				item = new Item();
				break;
		}
		item.m_guid = guid;//ID 唯一标识
		item.m_key =  key;
		item.m_name = name;
		item.m_iconId = iconId;
		item.m_border = bordered;//边框
		item.m_descl = desc;//描述
		item.m_maxHeapNum = MaxHeapNum;//最大堆叠数目
		item.m_maxHoldNum = MaxHoldNum;//最大可持有数目
		item.m_num = num;//道具数目
		item.m_birthday =  Birthday;//道具创建日期
		item.m_lifecycle = Lifecycle;//有效时间
		item.m_quality = Quality;//道具品质，稀有度
		item.m_operateType = OperateType;//道具的操作类型
		item.m_type = Type;//道具类型
		item.m_subtype = SubType;//道具子类型
		item.m_bindingType = BindingType;//道具绑定类型
		item.m_bindingState = BindingState;//道具绑定状态
		item.m_maxUnbindingTimes = MaxUnbindingTimes;//最大可解绑次数
		item.m_usedUnbindingTimes = UsedUnbindingTime;//已经使用了的解绑次数
		item.m_goldPrice = Goldprice;//道具卖店价格
		item.m_zhangongPrice = zhangongPrice;//战功售价
		item.m_yuanbaoPrice = yuanbaoPrice;//元宝售价
		item.m_banggongPrice = banggongPrice;//帮贡售价
		item.m_minLevelLimit =  minLevelLimit;//最小等级限制
		item.m_maxLevelLimit = maxLevelLimit;//最大等级限制
		item.m_jobLimit = jobLimit;
		item.m_label_left = lbleft;
		item.m_label_right = lbright;
//		Tools.debugPrintln("item.type="+item.m_type+";item.m_name="+item.m_name+";item.m_iconId="+item.m_iconId+";item.m_num="+item.m_num);
		item.m_icon = IconManger.getInstance().getIcon(iconId);
		item.m_bangding = IconManger.m_bangding;
		if(jobLimit > 0 && jobLimit != DWGameManager.getInstance().m_role.m_vocation){
			item.m_isLimit = true;
		}
		
		UIFixWindow.setItemFix(item);	
		return item;
	}
    
	public void drawIcon(DWGraphics g, int x, int y, int w, int h, int anchor)
	{
		int icon_w = 0;
		int icon_h = 0;
		int tempX = 0;
		int tempY = 0;
		if( m_icon != null ){
			icon_w = m_icon.getWidth();
			icon_h = m_icon.getHeight();			
			g.drawBitmap(m_icon, x, y, anchor);
		}
		if(m_isLimit){
			Bitmap img = IconManger.m_isLimit;
			if(img == null){
				return;
			}
			icon_w = img.getWidth();
			icon_h = img.getHeight();			
			g.drawBitmap(img, x, y, anchor);
		}
		if( m_pinzhi != null && !m_dpinzhi){
			g.drawBitmap(m_pinzhi, x, y, w,h,anchor);
		}
		if( m_bindingState == ItemConstants.绑定状态_是 && m_bangding != null && !m_dbangding){
			int bd_w = m_bangding.getWidth();
			int bd_h = m_bangding.getHeight();
			tempX = x + (Math.abs(icon_w - bd_w)>> 1);
			tempY = y + (Math.abs(icon_h - bd_h)>> 1);
			g.drawBitmap(m_bangding, tempX, tempY, anchor);
		}
		if(!m_showCD && this instanceof ItemConsume && this.m_subtype == ItemConstants.消耗_血蓝药){
			ItemConsume item = (ItemConsume)this;
			Hashtable<String, Long[]> hash = DWGameManager.getInstance().m_role.m_item_cd;
			if(hash == null){
				return;
			}
			String key = String.valueOf(item.m_cd_type);
			//绘制CD计时蒙版
			if ( item.m_time_cd > 0 && hash.containsKey(key)){
				long lastTime = (hash.get(key)[0]).longValue();
				long time = System.currentTimeMillis() - lastTime;
				long cd = item.m_time_cd * DWGameManager.SECOND;
				if (time < cd) {
					g.setColor(SkillConstants.COLOR_CD_COVER);
					if ((anchor & DWGraphics.RIGHT) != 0)
						x -= icon_w;
					if ((anchor & DWGraphics.HCENTER) != 0)
						x -= icon_w / 2;
					if ((anchor & DWGraphics.BOTTOM) != 0)
						y -= icon_h;
					if ((anchor & DWGraphics.VCENTER) != 0)
						y -= icon_h / 2;
					g.fillArc(x, y, icon_w, icon_h, -90 + time * 360 / cd, 360 - time * 360 / cd);
				}
//				g.fillArc(x, y, w, h, 90 - (m_cd - m_cd_counter) * 360 / m_cd, -m_cd_counter * 360 / m_cd);
			}
		}
	}
	
	public void drawIcon(DWGraphics g, int x, int y, int w, int h, float scaleX, float scaleY, int anchor)
	{
		int icon_w = 0;
		int icon_h = 0;
		int tempX = 0;
		int tempY = 0;
		if (m_icon != null) {
			icon_w = (int) (m_icon.getWidth() * scaleX);
			icon_h = (int) (m_icon.getHeight() * scaleY);
			g.drawBitmap(m_icon, x, y, icon_w, icon_h, anchor);
		}
		if (m_isLimit) {
			Bitmap img = IconManger.m_isLimit;
			if(img == null){
				return;
			}
			icon_w = (int) (img.getWidth() * scaleX);
			icon_h = (int) (img.getHeight() * scaleY);
			g.drawBitmap(img, x, y, icon_w, icon_h, anchor);
		}
		if (m_pinzhi != null && !m_dpinzhi) {
			int pinzhi_w = (int) (m_pinzhi.getWidth() * scaleX);
			int pinzhi_h = (int) (m_pinzhi.getHeight() * scaleY);
			g.drawBitmap(m_pinzhi, x, y, pinzhi_w, pinzhi_h, anchor);
		}
		if( m_bindingState == ItemConstants.绑定状态_是  && m_bangding != null && !m_dbangding){
			int bd_w = (int)(m_bangding.getWidth()*scaleX);
			int bd_h = (int)(m_bangding.getHeight()*scaleY);
			tempX = x + (Math.abs(icon_w - bd_w)>> 1);
			tempY = y + (Math.abs(icon_h - bd_h)>> 1);
			g.drawBitmap(m_bangding, tempX, tempY, bd_w,bd_h,anchor);
		}
	}
	
	/**
	 * @exception 判断当前道具是否允许此类型操作
	 * @param byte 操作类型
	 * */
	public boolean isOperate(byte operateType){
		return (m_operateType & operateType) != 0;
	}

	public boolean doShortcutAction()
	{
		return true;
	}

	public boolean doShortcutAction(Role role)
	{
		return true;
	}
}
